/**
 * 用于统计页面的访问深度
 * @author lamwood
 * @type Boolean
 */
var tj_load = tj_middle = tj_bottom = true;
var tj_domain = '//'+get('dm') || '';
var tj_url = tj_domain+'/index/tongji/index.html';
var tj_vcid = get('vcid') || 0;
//
function post(url, data){
    var senddata = "";
    for(var i in data){
        senddata = senddata+i+"="+data[i]+"&";
    }
    senddata = senddata + "timp="+new Date().getTime();
    //
    var xhr = null;
    if(XMLHttpRequest){
        xhr = new XMLHttpRequest()
    }else{
        new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhr.open("post", url, true);
    xhr.setRequestHeader('content-type','application/x-www-form-urlencoded');
    xhr.send(senddata);
    xhr.onreadystatechange = function(){
        if(xhr.readyState === 4 && xhr.status === 200){
            //console.log(xhr.responseText);
        }
    }
}
//
function get(name){
    var script;
    if(document.currentScript){
        script = document.currentScript;
    }else{
        var scripts = document.getElementsByTagName('script');
        script = scripts[scripts.length - 1];
    }
    var url = script.hasAttribute ? script.src : script.getAttribute('src');url = script.getAttribute('src');
    var query = url.split("?");
    if(query.length > 1){
        var vars = query[1].split("&");
        for(var i=0; i<vars.length; i++){
            var pair = vars[i].split("=");
            if(pair[0] === name){
                return pair[1];
            }
        }
    }
    return '';
}
//
if(tj_load){
    tj_load = false;
    post(tj_url, {position:1,vcid:tj_vcid});
}
//
window.onscroll = function(){
    //scrollTop就是触发滚轮事件时滚轮的高度
    //var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    var scrollPosition = window.pageYOffset;  //Netscape属性，指的是滚动条顶部到网页顶部的距离
    var windowSize     = window.innerHeight;   //窗口高度
    var bodyHeight     = document.body.scrollHeight; //页面长度
    var midDistance    = parseInt(bodyHeight/2);   //页面滚动到一半高度
    var hSize = parseInt(windowSize / 2); //窗口一半的高度
    //
    if(scrollPosition >= bodyHeight - windowSize - hSize){//底部
        if(tj_bottom){
            if(tj_middle){
                tj_middle = false;
                post(tj_url, {position:2,vcid:tj_vcid});
            }
            tj_bottom = false;
            post(tj_url, {position:3,vcid:tj_vcid});
        }
    }else if(midDistance - hSize  <= scrollPosition && scrollPosition < midDistance + hSize){//中部
        if(tj_middle){
            tj_middle = false;
            post(tj_url, {position:2,vcid:tj_vcid});
        }
    }
}